﻿using System.Collections.Generic;
using System.Linq;
using Harvest.Api.Model;
using Harvest.Api.Model.Internal;
using Harvest.Api.Types;

namespace Harvest.Api.ExtensionMethods
{

    public static class TaskExtensions
    {

        public static Task ToTask(this TaskItem entity)
        {
            var result = new Task
            {
                BillableByDefault = entity.BillableByDefault,
                Deactivated = entity.Deactivated,
                DefaultHourlyRate = entity.DefaultHourlyRate.HasValue ? entity.DefaultHourlyRate.Value : (double?)null,
                Id = entity.Id.HasValue ? entity.Id.Value : (int?)null,
                IsDefault = entity.IsDefault,
                Name = entity.Name,
                Billable = entity.Billable
            };
            return result;
        }


        public static TaskItem ToTaskItem(this Task entity)
        {
            var id = new NullableInt();
            if (entity.Id.HasValue) id = entity.Id.Value;

            var defaultHourlyRate = new NullableDouble();
            if (entity.DefaultHourlyRate.HasValue) defaultHourlyRate = entity.DefaultHourlyRate.Value;

            var result = new TaskItem
            {
                BillableByDefault = entity.BillableByDefault,
                Deactivated = entity.Deactivated,
                DefaultHourlyRate = defaultHourlyRate,
                Id = id,
                IsDefault = entity.IsDefault,
                Name = entity.Name,
                Billable = entity.Billable
            };
            return result;
        }


        public static List<Task> ToTasks(this IEnumerable<TaskItem> entity)
        {
            var result = entity.Select(x => x.ToTask()).ToList();
            return result;
        }


        public static List<TaskItem> ToTasksItem(this IEnumerable<Task> entity)
        {
            var result = entity.Select(x => x.ToTaskItem()).ToList();
            return result;
        }
    }

}
